// pages/sign up/signup.js
import {
    Activity_apply
} from '../../model/activity_signup.js'
const activity_apply = new Activity_apply();
import {
    Apply
} from '../../model/apply.js'
const apply = new Apply();
var WxParse = require("../../wxParse/wxParse.js")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        //轮播图
        imgUrls: [],
        indicatorDots: false,
        autoplay: true,
        interval: 5000,
        duration: 500,
        form_text: {},
        number: 0,
        registeredNum: 0,
        article: '',
        lon: '',
        lat: '',
        detailAddress: '',
        isJoin: 0,
        type: 0,
    },
    // go:function(){
    //     wx.navigateTo({
    //       url: '/pages/apply/apply',
    //     })
    // },

    //返回
    handlerGobackClick() {
        wx.reLaunch({
            url: '../firstpage/firstpage'
        })

    },
    //报名
    apply: function (e) {
        console.log(e)
        if (this.data.form_text.joinType == 2) {
            var obj=[]
            var parmes = {
                id:this.data.form_text.id,
                json:JSON.stringify(obj)
            }
            this.apply_yes(parmes)
        } else {
            this.applyForm()
        }
    },
    //报名
    apply_yes(parmes) {
        var that = this
        apply.join_activity(parmes).then(res => {
            console.log(res)
            if (res.code == 0 && res.data.notice != '') {
                var form_text = that.data.form_text
                form_text.isJoin=1
                that.setData({
                    form_text: form_text
                })
                wx.showModal({
                    cancelColor: 'cancelColor',
                    title: '提示',
                    content: res.data.notice,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            // console.log('用户点击确定')
                        }
                    }
                })

            } else if (res.code == 1) {
                wx.showModal({
                    cancelColor: 'cancelColor',
                    title: '提示',
                    content: res.msg,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        }
                    }
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1000,
                })
                return false
            }
        }).catch(error => {
            console.log(error)
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: options.id
        })
        if (options.type == 1) {
            this.setData({
                type: 1
            })
        }
        this.activity_signup()
    },
    //活动报名详情
    activity_signup() {
        var obj = {
            id: this.data.id
        };
        var that = this;
        activity_apply.activity_detail(obj).then(res => {
            this.setData({
                imgUrls: res.data.picList,
                form_text: res.data.datum,
                number: res.data.datum.number,
                registeredNum: res.data.datum.registeredNum,
                lon: res.data.datum.lon,
                lat: res.data.datum.lat,
                detailAddress: res.data.datum.detailAddress,
                isJoin: res.data.datum.isJoin
            })
            console.log(res.data.datum.content)
            var article = '<p><img src="https://qiniu.changetraining.com.cn/FnZu0v_QBnrkuHHzSW8_vWCtU5Jn" style="width:100%;height：300rpx;"/><img src="https://qiniu.changetraining.com.cn/FjY3m6bP_j3hxH_gcJFTKLlRNOlS" style="color: inherit; width: 100%;height：300rpx;"/></p><p></p><p></p>';
            
            console.log(article);
            WxParse.wxParse('article', 'html', article, that, 1)

        }).catch(error => {
            console.log(error)
        })
    },
    //地图
    tothere() {
        // console.log(e.currentTarget.dataset.item)
        // return false
        var that = this
        console.log('进入方法')
        wx.getSetting({
            success(res) {
                console.log('获取用户权限')
                wx.authorize({
                    scope: 'scope.userLocation',
                    success() {
                        // wx.chooseLocation({
                        //   success(res) {
                        //     console.log(res)
                        //   }
                        // })
                        wx.getLocation({ //获取当前经纬度
                            type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息
                            success: function (res) {
                                console.log(res)
                                const lat = that.data.lat
                                const lon = that.data.lon
                                console.log(lat, lon)
                                wx.openLocation({ //​使用微信内置地图查看位置。
                                    latitude: Number(lat), //要去的纬度-地址
                                    longitude: Number(lon), //要去的经度-地址
                                    name: that.data.detailAddress,
                                    address: that.data.detailAddress
                                })
                            }
                        })

                    }
                })
                if (!res.authSetting['scope.userLocation']) {
                    wx.showModal({
                        title: '提示',
                        content: '您未允许小程序获取您的位置信息，可以在小程序设置界面（「右上角」 - 「设置」）中打开对该小程序的授权状态。',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                }
            }
        })
    },


    //报名表单
    applyForm() {
        var that = this
        var obj = {
            id: that.data.id
        }
        apply.activity_question(obj).then(res => {
            console.log(res)
            if (res.code == 0) {
                wx.navigateTo({
                    url: '/pages/apply/apply?id=' + that.data.id + '&isJoin=' + that.data.isJoin,
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1000,
                })
                // var form_text=that.data.form_text
                // form_text.isJoin=4
                // that.setData({
                //   form_text:form_text
                // })
            }
        }).catch(error => {
            wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 1000,
            })
            // var form_text=that.data.form_text
            // form_text.isJoin=4
            // that.setData({
            //   form_text:form_text
            // })
            console.log(error)
        })

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})